package com.jshoperxms.controller.api.langdu;

import com.jshoperxms.Response;
import com.jshoperxms.components.redis.RedisUtils;
import com.jshoperxms.controller.api.langdu.pojo.MineJoinTeam;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.MemberT;
import com.jshoperxms.entity.YdArticleRecordRdT;
import com.jshoperxms.entity.YdTeamMemberT;
import com.jshoperxms.entity.YdTeamT;
import com.jshoperxms.service.MemberTService;
import com.jshoperxms.service.YdArticleRecordRdTService;
import com.jshoperxms.service.YdTeamMemberTService;
import com.jshoperxms.service.YdTeamTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Member;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 2018/12/5.
 * Des:我的小队
 */
@RestController
@RequestMapping("/api/minecenter/team")
public class MineTeamController extends BaseTController {


    @Resource
    private YdTeamTService ydTeamTService;

    @Resource
    private MemberTService memberTService;

    @Resource
    private YdTeamMemberTService ydTeamMemberTService;

    @Resource
    private YdArticleRecordRdTService ydArticleRecordRdTService;

    @RequestMapping(value="/updateYdTeamMemberHead",method= RequestMethod.POST)
    public ResponseEntity tt(){
        List<YdTeamMemberT>list=this.ydTeamMemberTService.findAll(YdTeamMemberT.class);
        for(YdTeamMemberT ydTeamMemberT:list){
            Criterion criterion=Restrictions.eq("wxopenid",ydTeamMemberT.getWxopenid());
            MemberT memberT=this.memberTService.findOneByCriteria(MemberT.class,criterion);
            if(memberT!=null){
                ydTeamMemberT.setAvatarurl(memberT.getHeadpath());
                ydTeamMemberT.setTmnick(memberT.getNick());
                this.ydTeamMemberTService.update(ydTeamMemberT);
            }
        }
        return Response.success("2");
    }

    /**
     * 查询创建的小队
     * @param rq
     * @return
     */
    @RequestMapping(value="/findMyCreatedTeam",method= RequestMethod.POST)
    public ResponseEntity findMyCreatedTeam(@RequestBody YdTeamT rq){
        Map<String,Object> responsedata=new HashMap<>();
        if(StringUtils.isNotBlank(rq.getWxopenid())){
            Criterion criterion= Restrictions.and(Restrictions.eq("wxopenid",rq.getWxopenid())).add(Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState()));
            List<YdTeamT>list=this.ydTeamTService.findByCriteriaByPage(YdTeamT.class,criterion, Order.desc(TagWords.UPDATETIME),1,100);
            responsedata.put("results",list);
            responsedata.put(TagWords.SUCCESS,true);
        }else{
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }
        return Response.success(responsedata);
    }


    /**
     * 我加入的小队
     * @param rq
     * @return
     */
    @RequestMapping(value="/findMyJoinedTeam",method= RequestMethod.POST)
    public ResponseEntity findMyJoinedTeam(@RequestBody YdTeamT rq) {
        Map<String, Object> responsedata = new HashMap<>();
        if (StringUtils.isNotBlank(rq.getWxopenid())) {
            //String sql="select tt.id,tt.teamname,tt.teamdes,tt.maxmember,tt.currentmember,tt.leadername,tt.teamavatarurl,tt.teamcategory,tt.teamno,tt.createtime,tt.totalyuedu,tt.creatorid from yd_team_member_t as tm ,yd_team_t as tt where tm.teamid=tt.id and tm.wxopenid =:wxopenid";
            String sql="select * from yd_team_t as tt left join yd_team_member_t as tm on tt.id=tm.teamid where tm.wxopenid=:wxopenid and tt.wxopenid!=:wxopenid and tm.status=1";
            Map<String,Object>params=new HashMap<>();
            params.put("wxopenid",rq.getWxopenid());
            List<YdTeamT>list=this.ydTeamTService.queryBySql(YdTeamT.class,sql,params);
            responsedata.put("results", list);
            responsedata.put(TagWords.SUCCESS, true);
        }else {
            responsedata.put(TagWords.SUCCESS, false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }
        return Response.success(responsedata);
    }


    /**
     * 退出小队
     * @param rq
     * @return
     */
    @RequestMapping(value="/quitTeam",method= RequestMethod.POST)
    public ResponseEntity quitTeam(@RequestBody YdTeamMemberT rq){
        Map<String, Object> responsedata = new HashMap<>();
        if (StringUtils.isNotBlank(rq.getWxopenid())&&StringUtils.isNotBlank(rq.getTeamid())) {
            Criterion criterion=Restrictions.and(Restrictions.eq("wxopenid",rq.getWxopenid())).add(Restrictions.eq("teamid",rq.getTeamid()));
            YdTeamMemberT ydTeamMemberT=this.ydTeamMemberTService.findOneByCriteria(YdTeamMemberT.class,criterion);
            if(ydTeamMemberT!=null){
                ydTeamMemberT.setStatus(BaseEnums.DataUsingState.DEL.getState());
                ydTeamMemberT.setVersiont(ydTeamMemberT.getVersiont()+1);
                ydTeamMemberT.setUpdatetime(DateTools.getSystemTimeLong());
                this.ydTeamMemberTService.update(ydTeamMemberT);
                responsedata.put(TagWords.SUCCESS, true);
            }
        }else{
            responsedata.put(TagWords.SUCCESS, false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }
        return Response.success(responsedata);
    }



    /**
     * 我的图文朗读记录
     * @param rq
     * @return
     */
    @RequestMapping(value="/findMineTeamArticleRecords",method= RequestMethod.POST)
    public ResponseEntity findMineYdArticleRecordsList(@RequestBody SearchModel rq){
        Map<String,Object> responsedata=new HashMap<>();
        Criterion criterion= Restrictions.and(Restrictions.eq("wxopenid",rq.getWxopenid())).add(Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState())).add(Restrictions.eq("teamid",rq.getTeamid()));
        int currentPage=rq.getStart();
        int lineSize=rq.getLength();
        int records=this.ydArticleRecordRdTService.count(YdArticleRecordRdT.class,criterion).intValue();
        List<YdArticleRecordRdT> list=this.ydArticleRecordRdTService.findByCriteriaByPage(YdArticleRecordRdT.class,criterion, Order.desc(TagWords.CREATETIME),currentPage,lineSize);
        rq.setRecordsTotal(records);
        responsedata.put("results",list);
        responsedata.put("start",rq.getNextPageNo());
        responsedata.put("totalPage",rq.getTotalPage());
        responsedata.put("totalrecords",records);
        responsedata.put(TagWords.SUCCESS,true);
        return Response.success(responsedata);
    }




}
